﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Production_Get.API.Application.Commands;
using Production_Get.API.Application.Commands.CustomerShow;
using Production_Get.API.Application.Commands.OrderShow;
using Production_Get.API.Application.Commands.Warehousingtes;
using Production_Get.API.Infrastructures.Interfaces;
using SlaughterItem.Common.Infrastructures.Interfaces;
using System.Collections.Generic;
using System.Security.Cryptography;
using THE_WIND_RISES.Common.Domans.Produce.Customers;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Controllers.LEO
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShowProductionController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<ShowProductionController> logger;
        private readonly IBaseRepositoryGet<OrdersDto> OrdersDto;
        private readonly IBaseRepositoryGet<CustomerDto> CustomerDto;
        private readonly IBaseRepositoryGet<StorageDto> StorageDto;

        public ShowProductionController(IMediator mediator, ILogger<ShowProductionController> logger, IBaseRepositoryGet<OrdersDto> OrdersDto,
            IBaseRepositoryGet<CustomerDto> CustomerDto, IBaseRepositoryGet<StorageDto> StorageDto)
        {
            this.mediator = mediator;
            this.logger = logger;
            this.OrdersDto = OrdersDto;
            this.CustomerDto = CustomerDto;
            this.StorageDto = StorageDto;
        }


        /// <summary>
        /// 资源管理列表显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetResourceShow(ResourceScretenCommend cmd)
        {
            try
            {
                logger.LogInformation("资源管理列表显示成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("资源管理列表显示失败");
                throw;
            }

        }

        /// <summary>
        /// 资源管理反填  
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetResourceMana(ResourceManaCommend cmd)
        {
            try
            {
                logger.LogInformation("资源管理反填成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("资源管理反填失败");
                throw;
            }
        }

        /// <summary>
        /// 资源类型 
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetResourceTypeShow()
        {
            try
            {
                logger.LogInformation("资源类型成功");
                return await mediator.Send(new ResourceTypeScretenCommend());
            }
            catch (Exception)
            {
                logger.LogError("资源类型失败");
                throw;
            }

        }

        /// <summary>
        /// 费用类型
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetExpensesShow()
        {

            try
            {
                logger.LogInformation("费用类型成功");
                return await mediator.Send(new ExpensesScretenCommend());
            }
            catch (Exception)
            {
                logger.LogError("费用类型失败");
                throw;
            }
        }

        /// <summary>
        /// 基准类型  
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetBenchmarkShow()
        {
            try
            {
                logger.LogInformation("基准类型成功");
                return await mediator.Send(new BenchmarkScretenCommend());
            }
            catch (Exception)
            {
                logger.LogError("基准类型失败");
                throw;
            }

        }

        /// <summary>
        /// 客户管理列表显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetCustomerShow(CustomerSrcetenCommend cmd)
        {
            try
            {
                logger.LogInformation("客户管理显示成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("客户管理显示失败");
                throw;
            }

        }

        /// <summary>
        /// 客户管理反填显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetCustomerMang(CustomerMangCommend cmd)
        {
            try
            {
                logger.LogInformation("客户管理反填成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("客户管理反填失败");
                throw;
            }

        }

        /// <summary>
        /// 客户行业列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetIndustryShow(IndustrySrcetenCommend cmd)
        {
            try
            {
                logger.LogInformation("客户行业显示成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("客户行业显示失败");
                throw;
            }

        }

        /// <summary>
        /// 销售订单显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetOrdersShow(OrdersScretenCommend cmd)
        {
            try
            {
                logger.LogInformation("销售订单显示成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("销售订单显示失败");
                throw;
            }

        }

        /// <summary>
        /// 销售订单类型列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetOrdersTypeShow(OrdersTypeScretenCommend cmd)
        {
            try
            {
                logger.LogInformation("销售订单类型显示成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("销售订单类型显示失败");
                throw;
            }

        }

        /// <summary>
        /// 所属项目列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetProjectShow(ProjectScretenCommend cmd)
        {
            try
            {
                logger.LogInformation("所属项目显示成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("所属项目显示失败");
                throw;
            }

        }

        /// <summary>
        /// 销售订单反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetOrdersMang(OrdersMangCommend cmd)
        {
            try
            {
                logger.LogInformation("销售订单反填成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("销售订单反填失败");
                throw;
            }

        }

        /// <summary>
        /// 订单行列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetOrdersLineShow(OrderLineSrcetenCommend cmd)
        {
            try
            {
                logger.LogInformation("订单行列表成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("订单行列表失败");
                throw;
            }

        }

        /// <summary>
        /// 物料信息列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetItemInfosSrceten(ItemInfosSrcetenCommend cmd)
        {
            try
            {
                logger.LogInformation("物料信息列表成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("物料信息列表失败");
                throw;
            }

        }

        /// <summary>
        /// 订单行反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetOrdersLineMan(OrderLineManCommend cmd)
        {
            try
            {
                logger.LogInformation("订单行反填成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("订单行反填失败");
                throw;
            }

        }



        /// <summary>
        /// 订单导出所有的信息为
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportExcel()
        {
            try
            {
                string sql = "SELECT * FROM Orders a INNER JOIN OrdersType b on a.OrdersTypeId=b.OrdersTypeId INNER JOIN Project c on a.ProjectId=c.ProjectId " +
                "INNER JOIN Customer d on a.CustomerId=d.CustomerId INNER JOIN Department e on a.DepartmentId=e.DepartmentId where a.IsDelete=0";
                OrdersDto come = new OrdersDto();
                var list = OrdersDto.GetAll(sql, come).ToList();

                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet("Sheet1");
                // 第一行 标题
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("订单编号");
                row.CreateCell(2).SetCellValue("订单名称");
                row.CreateCell(3).SetCellValue("订单类型");
                row.CreateCell(4).SetCellValue("所属项目");
                row.CreateCell(5).SetCellValue("客户");
                row.CreateCell(6).SetCellValue("签订时间");
                row.CreateCell(7).SetCellValue("销售员");
                row.CreateCell(8).SetCellValue("销售部门");
                row.CreateCell(9).SetCellValue("操作人");
                row.CreateCell(10).SetCellValue("操作时间");
                // 第二行 写数据
                int i = 1;
                foreach (var item in list)
                {
                    row = sheet.CreateRow(i);
                    row.CreateCell(0).SetCellValue(item.OrdersId);
                    row.CreateCell(1).SetCellValue(item.OrdersNo);
                    row.CreateCell(2).SetCellValue(item.OrdersName);
                    row.CreateCell(3).SetCellValue(item.OrdersTypeName);
                    row.CreateCell(4).SetCellValue(item.ProjectName);
                    row.CreateCell(5).SetCellValue(item.CustomerName);
                    row.CreateCell(6).SetCellValue(item.SignedTime.ToString("yyyy-MM-dd-HH-mm-ss"));
                    row.CreateCell(7).SetCellValue(item.Salesman);
                    row.CreateCell(8).SetCellValue(item.DepartmentName);
                    row.CreateCell(9).SetCellValue(item.Handle);
                    row.CreateCell(10).SetCellValue(item.HandleTime.ToString("yyyy-MM-dd-HH-mm-ss"));
                    i++;
                }
                // 写 WorkBook信息到 内存流中
                byte[] buffer = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                // .xlsx文件对应的Mime信息
                var mime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(buffer, mime, "学生信息.xlsx");

            }
            catch (Exception)
            {
                throw;
            }
        }





        /// <summary>
        /// 客户下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetCustome()
        {
            try
            {
                logger.LogInformation("客户下拉成功");
                return await mediator.Send(new CustomerShowCommend());
            }
            catch (Exception)
            {
                logger.LogError("客户下拉失败");
                throw;
            }
        }

        /// <summary>
        /// 部门下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetDepartment()
        {
            try
            {
                logger.LogInformation("部门下拉成功");
                return await mediator.Send(new DepartmentShowCommend());
            }
            catch (Exception)
            {
                logger.LogError("部门下拉失败");
                throw;
            }
        }

        /// <summary>
        /// 客户导出所有的信息为Excel
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult CusExportExcel()
        {
            try
            {
                string sql = "SELECT * FROM Customer a INNER JOIN Industry b on a.IndustryId=b.IndustryId where a.IsDelete=0";
                CustomerDto come = new CustomerDto();
                var list = CustomerDto.GetAll(sql, come).ToList();

                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet("Sheet1");
                // 第一行 标题
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("客户编号");
                row.CreateCell(2).SetCellValue("客户名称");
                row.CreateCell(3).SetCellValue("客户类型");
                row.CreateCell(4).SetCellValue("客户地址");
                row.CreateCell(5).SetCellValue("客户电话");
                row.CreateCell(6).SetCellValue("所在城市");
                row.CreateCell(7).SetCellValue("创建时间");
                row.CreateCell(8).SetCellValue("创建人");
                // 第二行 写数据
                int i = 1;
                foreach (var item in list)
                {
                    row = sheet.CreateRow(i);
                    row.CreateCell(0).SetCellValue(item.CustomerId);
                    row.CreateCell(1).SetCellValue(item.CustomerNo);
                    row.CreateCell(2).SetCellValue(item.CustomerName);
                    row.CreateCell(3).SetCellValue(item.CustomerType);
                    row.CreateCell(4).SetCellValue(item.CustomerAddress);
                    row.CreateCell(5).SetCellValue(item.CustomerPhone);
                    row.CreateCell(6).SetCellValue(item.CityId);
                    row.CreateCell(7).SetCellValue(item.HandleTime.ToString());
                    row.CreateCell(8).SetCellValue(item.Handle);
                    i++;
                }
                // 写 WorkBook信息到 内存流中
                byte[] buffer = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                // .xlsx文件对应的Mime信息
                var mime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(buffer, mime, "学生信息.xlsx");

            }
            catch (Exception)
            {
                throw;
            }
        }
        


        /// <summary>
        /// 入库列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetStorageSrceten(StorageSrcetenCommend cmd)
        {
            try
            {
                logger.LogInformation("入库列表成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("入库列表失败");
                throw;
            }
        }

        /// <summary>
        /// 入库反填
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetStorageMan(StorageManCommend cmd)
        {
            try
            {
                logger.LogInformation("入库反填成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("入库反填失败");
                throw;
            }
        }

        /// <summary>
        /// 仓库下拉
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetWarehouseSrceten(WarehouseSrcetenCommend cmd)
        {
            try
            {
                logger.LogInformation("仓库下拉成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("仓库下拉失败");
                throw;
            }
        }

        /// <summary>
        /// 委外订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetPurchasings(PurchasingsSrcetenCommend cmd)
        {
            try
            {
                logger.LogInformation("委外订单成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("委外订单失败");
                throw;
            }
        }

        /// <summary>
        /// 委外反填
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetPurchasingsMan(PurchasingsManCommend cmd)
        {
            try
            {
                logger.LogInformation("委外反填成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("委外反填失败");
                throw;
            }
        }

        /// <summary>
        /// 订单行列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetOrderTineShow(OrderTineSrcetenCommend cmd)
        {
            try
            {
                logger.LogInformation("订单行列表成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("订单行列表失败");
                throw;
            }
        }

        /// <summary>
        /// 订单行反填
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetOrderTineMan(OrderTineManCommend cmd)
        {
            try
            {
                logger.LogInformation("订单行反填成功");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                logger.LogError("订单行反填失败");
                throw;
            }
        }

        /// <summary>
        /// 入库单导出所有的信息为Excel
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult CusStorageExcel()
        {
            try
            {
                string sql = "SELECT * FROM Storage a INNER JOIN PurchasingType b on a.PurchasingTypeId=b.PurchasingTypeId INNER JOIN Purchasings c on a.PurchasinId=c.PurchasinId" +
                   " INNER JOIN Warehouse d on a.WarehouseId = d.WarehouseId where a.IsDelete = 0";
                StorageDto come = new StorageDto();
                var list = StorageDto.GetAll(sql, come).ToList();

                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet("Sheet1");
                // 第一行 标题
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("入库单编码");
                row.CreateCell(2).SetCellValue("入库来源名称");
                row.CreateCell(3).SetCellValue("来源头编码");
                row.CreateCell(4).SetCellValue("入库类型");
                row.CreateCell(5).SetCellValue("仓库名称");
                row.CreateCell(6).SetCellValue("入库日期");
                row.CreateCell(7).SetCellValue("制单人");
                row.CreateCell(8).SetCellValue("制单部门");
                // 第二行 写数据
                int i = 1;
                foreach (var item in list)
                {
                    row = sheet.CreateRow(i);
                    row.CreateCell(0).SetCellValue(item.StorageId);
                    row.CreateCell(1).SetCellValue(item.StorageNo);
                    row.CreateCell(2).SetCellValue(item.PurchasingTypeName);
                    row.CreateCell(3).SetCellValue(item.PurchasingNo);
                    row.CreateCell(4).SetCellValue(item.StorageType);
                    row.CreateCell(5).SetCellValue(item.WarehouseName);
                    row.CreateCell(6).SetCellValue(item.StorageDate.ToString());
                    row.CreateCell(7).SetCellValue(item.MakeSingle);
                    row.CreateCell(8).SetCellValue(item.MakeDepartment);
                    i++;
                }
                // 写 WorkBook信息到 内存流中
                byte[] buffer = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                // .xlsx文件对应的Mime信息
                var mime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(buffer, mime, "入库单信息.xlsx");

            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
